Brief Full Advanced Search Search Tips
|  |  | To access the contents, click the chapter and section titles. Visual Basic 6 Programming Blue Book: The Most Complete, Hands-On Resource for Writing Programs with Microsoft Visual Basic 6! (Publisher: The Coriolis Group) Author(s): Peter G. Aitken ISBN: 1576102815 Publication Date: 08/01/98 Search this book:
Previous | Table of Contents | Next
We can write a simple program to demonstrate how you can use the Drives collection to obtain information about the drives on the system. Create a Standard EXE project and place a Text Box on the form. Set the Text Box’s Multiline and Locked properties to True and set its size to nearly fill the form. Put the code from Listing 14.1 in the Text Box’s Click event procedure. When you run the program, click on the Text Box. After a brief pause it will display a list of the system’s drives and their total and free space. This is shown in Figure 14.1. I named this program DRIVESDEMO. Listing 14.1 Demonstrating the Drives collection.
    Private Sub Text1_Click()

    Dim fs, d, dc
    Dim msg As String

    Set fs = CreateObject(“Scripting.FileSystemObject”)
    Set dc = fs.Drives
    For Each d In dc
        msg = msg & “Drive ” & d.Path
        If Not d.IsReady Then
            msg = msg & “ is not ready.” & vbCrLf
        Else
            msg = msg & vbCrLf & Space(5)
            msg = msg & “Total space: ” & FormatNumber(d.TotalSize)
            msg = msg & vbCrLf & Space(5)
            msg = msg & “Free space: ” & FormatNumber(d.FreeSpace)
            msg = msg & vbCrLf
        End If
    Next

    Text1.Text = msg

    End Sub
#### The Folder Object A Folder object represents a single folder, or subdirectory, on a drive. You use the object’s methods to copy, move, or delete the folder (as explained later) and the object’s properties to obtain information about the folder. Perhaps most important, a Folder object contains two collections, Files and SubFolders, that provide access to the files and subfolders within the folder. Table 14.3 explains the properties of the Folder object. Because each Folder object contains information about its parent folder and its subfolders, you can easily traverse the entire folder structure on a drive. This is a powerful tool, as will be demonstrated later. First, however, we need to look at the Folder object’s methods. The Copy method copies the folder and its contents to a new location. The syntax is (assuming f to be a Folder object)
    f.Copy destination[, overwrite]
where destination specifies the destination where the folder is to be copied. Set overwrite to True (the default) to overwrite existing files or folders, or to False otherwise. Note that you can also copy a folder using the FileSystemObject’s CopyFolder method. The Move method moves the folder and its contents from one location to another. The syntax is
    f.Move destination
where destination specifies the destination where the folder is to be moved. You can also move folders with the FileSystemObject’s MoveFolder method. Figure 14.1 Using the Drives collection to obtain information about the system’s drives.
Table 14.3 Properties of the Folder object.
Property | Description
--- | ---
DateCreated | Date and time the folder was created.
DateLastAccessed | Date and time the folder was last accessed.
DateLastModified | Date and time the folder was last modified.
Drive | Drive letter of the drive where the folder resides.
Files | A Files collection containing all the files in the folder.
IsRootFolder | True if the folder is the root, False otherwise.
Name | Sets or returns the name of the folder.
ParentFolder | Returns a Folder object representing the folder’s parent folder.
Path | The path of the folder, including the drive letter.
ShortName | The short name used by programs that require the old 8.3 style naming convention.
ShortPath | The short path used by programs that require the old 8.3 style naming convention.
Size | The size, in bytes, of all files and subfolders contained in the folder.
SubFolders | A Folders collection containing one Folder object for each subfolder.
The Delete method deletes a folder and its contents. The syntax is:
    f.Delete [force]
The optional force argument specifies whether files or folders with the read-only attribute are to be deleted (force = True) or not (force = False, the default). You can also delete folders with the FileSystemObject’s DeleteFolder method. A Folder Demonstration To demonstrate the power of the Folder object, I have created a small utility that counts the total number of files and folders on your C: drive. This might seem like a difficult task, but as you’ll see, it requires relatively little code. Figure 14.2 shows the program in operation. The ease with which we can write this program is a result of two things: the design of the Folder object and the use of a recursive algorithm. Here’s an outline of how it works: 1. Get the drive’s root Folder object. 2. Use the root folder’s Folders collection to access each of its subfolders. Figure 14.2 The FOLDERDEMO program. 3. Determine the number of files and subfolders in each subfolder and add these values to the totals. 4. Use the subfolder’s Folders collection to access each of its subfolders. 5. Continue until all subfolders have been processed.
Previous | Table of Contents | Next

| Products | Contact Us | About Us | Privacy | Ad Info | Home Use of this site is subject to certain Terms & Conditions, Copyright © 1996-2000 EarthWeb Inc. All rights reserved. Reproduction whole or in part in any form or medium without express written permission of EarthWeb is prohibited. Read EarthWeb's privacy statement.